﻿using System.Configuration;
using System.Linq;

namespace MvcWechat.Common
{
    public class GetOpenMenu
    {
        public static bool IsOpenMenu(string menu){ 
            string key = CommonPage.getKey();
            string xml = "";
            bool isNewVersion = CommonPage.GetVersion(key);
            if (isNewVersion)
            {
                xml = CommonPage.readXMLFile(key);
            }
            else
            {
                //在判断缓存
                if (System.Web.HttpContext.Current.Cache.Get("Cache:xmlFile" + key) != null)
                {
                    xml = System.Web.HttpContext.Current.Cache.Get("Cache:xmlFile" + key).ToString();
                }
                else
                {
                    xml = CommonPage.readXMLFile(key);
                }
            }

            string[] str = xml.Split(',');
            var isExist = str.Where(p => p.Equals(menu)).ToList();
            if (isExist.Count == 1)
                return true;
            else
                return false;
        }

        public static bool IsDisplay(string unionMark)
        { 
            var openMenu = CommonPage.GetConfigSettings().Menus;
            bool isOpen = true;
            if (openMenu != null)
            {
                var openMenus = openMenu.Split('|');
                for (int i = 0; i < openMenus.Length; i++)
                {
                    if (openMenus[i].ToLower() == unionMark.ToLower())
                    {
                        isOpen = true;
                        break;
                    }
                    else isOpen = false;
                }

            }
            return isOpen;
        }
        /// <summary>
        /// 获取模板
        /// </summary>
        /// <returns></returns>
        public static string GetTemplateType()
        {
            return  ConfigurationManager.AppSettings["TemplateType"]??"1";
        }
    }
}